﻿using Aker.DevTeam;
using DevExpress.XtraReports.UI;
using System;
using System.Data;

namespace Aker
{
    public partial class OrderReport : XtraReport
    {
        string detailTableName;
        bool Eng = false;
        bool WithPictures = false;
        string Currency;
        int PriceType;

        public OrderReport(DataSet sourceData, bool secondCopy = false, bool eng = false, bool withPictures = false)
        {
            InitializeComponent();
            SecondCopy = secondCopy;
            Eng = eng;
            WithPictures = withPictures;

            if (!Eng)
            {
                x1.Text = "Marka";
                x2.Text = "Yetkili";
                x3.Text = "Adres";
                x4.Text = "Telefon";
                x5.Text = "e-Posta";
                x66.Text = "Fuar";
                x7.Text = "Sezon";
                x8.Text = "Kargo Bilgisi";
                tcPrivateNotesLabel.Text = "Özel Notlar";
                xrCancelled.Text = "İPTAL !!!";
                xrTableCell10.Text = "Ref. No.";
                xrDate.Text = "Tarih";
                lblPrivate.Text = "*** AKER KOPYASI ***";
                xrTableCell11.Text = "Notlar:";
                //tcNot.Text = "Minimum sipariş miktarı 300 metredir. Bu miktarın altındaki siparişler için $1,00/mt. ilave ücret uygulanır.";
                lblHNS.Text = "* HNS Müşterinin talebi üzerine gönderilmeyecek numuneleri ifade eder.";
                xrLabel2.Text = "";
                tcNot.Text = "";
                xrTableCell3.Text = "Artikel";
                xrTableCell7.Text = "Desen";
                xrTableCell6.Text = "Karışım";
                xrTableCell5.Text = xrTableCell5.Text.Replace("Width", "En");
                xrTableCell4.Text = xrTableCell4.Text.Replace("Weight", "Ağırlık");
                tcPriceHeader.Text = tcPriceHeader.Text.Replace("Price", "Fiyat");
                detailMeter.Text = "Metre";
                Notes.Text = "Notlar";
                xrTableCell1.Text = "MT.";
            }
            else
            {
                tcNotTR.Text = "";
            }




            ds = sourceData;
            detailTableName = ds.Tables[1].TableName;

            DataRow dr = ds.Tables[0].Rows[0];
            Currency = dr["Currency"].ToStringNN();
            if (Currency == "")
                Currency = Settings.Currency;

            PriceType = dr["PriceType"].ToIntNN();
            if (PriceType == -1)
                PriceType = UserSettings.PriceType;

            tcBrand.Text = dr["Brand"].ToStringNN();
            tcContact.Text = dr["Contact"].ToStringNN();
            tcAdres.Text = dr["Address"].ToStringNN();
            tcPhone.Text = dr["Phone"].ToStringNN();
            tcFair6.Text = dr["Fair"].ToStringNN();
            tcReference.Text = dr["Reference"].ToStringNN();
            lblOrderNotes.Text = dr["Notes"].ToStringNN();
            //tcFair.Text = dr["Notes"].ToString();
            tcCarrier.Text = dr["CarrierCustomerInfo"].ToStringNN();

            tcSeason.Text = dr["Season"].ToStringNN();
            tcMail.Text = dr["Email"].ToStringNN();
            tcPrivateNotes.Text = dr["PrivateNotes"].ToStringNN();

            if (dr["Showroom"] != DBNull.Value)
                if ((bool)dr["Showroom"] == true)
                    xrTableRow6.Visible = false;

            if (!SecondCopy)
            {
                trPrivate.Visible = false;
                lblPrivate.Visible = false;
            }

            //////if (Currency == "$")
            //////    tcPriceHeader.Text = tcPriceHeader.Text.Replace("€", "$");

            xrCancelled.Visible = (bool)dr["Cancelled"];

            ds.Tables[1].Columns.Add("RowNo", typeof(int));


            DataRowCollection rows = ds.Tables[1].Rows;
            for (int i = 0; i < rows.Count; i++)
                rows[i]["RowNo"] = i + 1;

            if (dr["EditDate"] != DBNull.Value)
                tcDate.Text = ((DateTime)dr["EditDate"]).ToString("dd.MM.yyyy HH:mm");
            else if (dr["InsertDate"] != DBNull.Value)
                tcDate.Text = ((DateTime)dr["InsertDate"]).ToString("dd.MM.yyyy HH:mm");
            else
                tcDate.Text = xrDate.Text = "";

            //tcRep.Text = AppSession.UserInfo.Name;
            tcRep.Text = dr["AdSoyad"].ToStringNN();

            if (ds.Tables[2].Rows[0][0].ToIntNN() < 1)
                lblHNS.Visible = false;

        }

        bool SecondCopy = false;


        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            this.DataSource = ds;
            this.DataMember = detailTableName;

            BindCell(detailArticle, "Article");
            BindCell(detailWeight, "Weight");
            BindCell(detailWidth, "Width");
            BindCell(detailComposition, "Composition");
            BindCell(detailDesign, "Design");
            //BindCell(detailPrice, "FiyatExtraEur", "{0:n}");

            //if (Currency == "$")
            //{
            //    if (PriceType == 0)
            //    {
            //        BindCell(detailPrice, "PriceDollar", "{0:n}");
            //    }
            //    else if (PriceType == 1) 
            //    {
            //        BindCell(detailPrice, "Price2Dollar", "{0:n}");
            //    }
            //    else if (PriceType == 2)
            //    {
            //        BindCell(detailPrice, "Price3Dollar", "{0:n}");
            //    }
            //}
            //else
            //{
            //    if (PriceType == 0)
            //    {
            //        BindCell(detailPrice, "PriceEuro", "{0:n}");
            //    }
            //    else if (PriceType == 1)
            //    {
            //        BindCell(detailPrice, "Price2Euro", "{0:n}");
            //    }
            //    else if (PriceType == 2)
            //    {
            //        BindCell(detailPrice, "Price3Euro", "{0:n}");
            //    }
            //}

            BindCell(detailMt, "Meter", "{0:n}");
            BindCell(detailRN, "RowNo");
            BindCell(detailNotes, "Notes");
        }

        private void BindCell(XRTableCell cell, string columnName, string format = "")
        {
            if (format == "")
                cell.DataBindings.Add("Text", null, detailTableName + "." + columnName);
            else
                cell.DataBindings.Add("Text", null, detailTableName + "." + columnName, format);
        }

        private void OrderReport_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (WithPictures)
                imgArticle.DataBindings.Add("Image", null, detailTableName + "." + "PhotoEmail");
            else
            {
                trDetail.Cells.Remove(detailPhoto);

                for (int i = 0; i < tblHeader.Rows[0].Cells.Count; i++)
                {
                    tblDetail.Rows[0].Cells[i].WidthF = tblHeader.Rows[0].Cells[i].WidthF;
                }

                trDetail.HeightF = 10;
                Detail.HeightF = 10;
            }
        }

    }
}